package view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import model.Group;
import model.Lesson;
import model.Student;
import model.Subject;

/**
 *
 * @author cska
 *
 */

public class LessonDlg extends JDialog implements ocDialog {

    protected Lesson lesson;
    protected List<Student> students;
    protected List<JCheckBox> checkBoxs;
    protected JPanel mainPanel;
    protected JScrollPane scrollPane;
    protected JButton ok, cancel;
    protected JButton all, none;
    protected JPanel bp1, bp2;
    protected JLabel l1, l2, l3, l4, l5;
    protected JComboBox type;
    protected JTextField theme;
    protected JTextField plan;
    protected JFormattedTextField date;
    public boolean fl;
    protected GridBagConstraints c;

    public LessonDlg(JFrame owner, boolean modal, String title, Subject subject, Group group) {
        super(owner, title, modal);
        setLayout(new GridBagLayout());

        lesson = new Lesson(subject, group, 0, "", "", getDate());
        students = group.getStudents();

        c = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        l1 = new JLabel("Тип:", JLabel.RIGHT);
        add(l1,c);
        c.gridy = 1;
        l2 = new JLabel("Тема:", JLabel.RIGHT);
        add(l2,c);
        c.gridy = 2;
        l3 = new JLabel("План:", JLabel.RIGHT);
        add(l3,c);
        c.gridy = 3;
        l4 = new JLabel("Дата:", JLabel.RIGHT);
        add(l4,c);

        c = new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        String types[] = {"лек.", "лаб.", "прак."};
        type = new JComboBox(types);
        add(type,c);
        c.gridy = 1;
        theme = new JTextField();
        theme.setHorizontalAlignment(JTextField.CENTER);
        add(theme,c);
        c.gridy = 2;
        plan = new JTextField();
        plan.setHorizontalAlignment(JTextField.CENTER);
        add(plan,c);
        c.gridy = 3;
        MaskFormatter mask = null;
        try {
            mask = new MaskFormatter("####.##.##");
        } catch (ParseException ex) {
            Logger.getLogger(TeacherWnd.class.getName()).log(Level.SEVERE, null, ex);
        }
        date = new JFormattedTextField(mask);
        date.setHorizontalAlignment(JFormattedTextField.CENTER);
        date.setText(lesson.getDate());
        add(date,c);

        c = new GridBagConstraints(0, 4, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        l5 = new JLabel("Отсутствующие", JLabel.CENTER);
        add(l5,c);

        c = new GridBagConstraints(0, 5, 2, 1, 1.0, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

        mainPanel = new JPanel(new GridBagLayout());
        scrollPane = new JScrollPane(mainPanel);
        add(scrollPane,c);

        c = new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        checkBoxs = new ArrayList<JCheckBox>();
        for (int i = 0; i < students.size(); ++i) {
            checkBoxs.add(new JCheckBox(students.get(i).getFio(), false));
            c.gridy = i;
            mainPanel.add(checkBoxs.get(i),c);
        }
        c.weighty = 1.0;
        c.gridy = students.size();
        c.fill = GridBagConstraints.BOTH;
        mainPanel.add(new JPanel(),c);

        c = new GridBagConstraints(0, 6, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        bp1 = new JPanel(new GridBagLayout());
        add(bp1,c);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.CENTER;
        all = new JButton("Выделить все");
        all.addActionListener(new allAdapter(this));
        bp1.add(all,c);
        c.gridx = 1;
        c.weightx = 0.0;
        none = new JButton("Снять выделение");
        none.addActionListener(new noneAdapter(this));
        bp1.add(none,c);

        c = new GridBagConstraints(0, 7, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        bp2 = new JPanel(new GridBagLayout());
        add(bp2,c);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.CENTER;
        ok = new JButton("Сохранить");
        ok.addActionListener(new okAdapter(this));
        bp2.add(ok,c);
        c.gridx = 1;
        c.weightx = 0.0;
        cancel = new JButton("Отмена");
        cancel.addActionListener(new cancelAdapter(this));
        bp2.add(cancel,c);

        setSize(400,480);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width-400)/2, (screen.height-480)/2);
        setResizable(false);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setVisible(true);
    }

    public void ok() {
        fl = true;
        lesson.setType(type.getSelectedIndex());
        lesson.setTheme(theme.getText());
        lesson.setPlan(plan.getText());
        lesson.setDate(date.getText());
        setVisible(false);
    }

    public void cancel() {
        fl = false;
        setVisible(false);
    }

    public Lesson getLesson() {
        return lesson;
    }

    public List<Student> getStudents() {
        List<Student> s = new ArrayList<Student>();
        for (int i = 0; i < students.size(); ++i)
            if (checkBoxs.get(i).isSelected())
                s.add(students.get(i));
        return s;
    }

    public String getDate() {
        Calendar cal = Calendar.getInstance();
        cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int month = (1 + cal.get(Calendar.MONTH));
        int year = cal.get(Calendar.YEAR);
        String date_str = year + "." + (month < 10 ? "0" + month : month ) + "." + (day < 10 ? "0" + day : day);
        return date_str;
    }

    public void selectAll() {
        for (JCheckBox box : checkBoxs)
            box.setSelected(true);
    }

    public void selectNone() {
        for (JCheckBox box : checkBoxs)
            box.setSelected(false);
    }

    class allAdapter implements ActionListener {

        protected LessonDlg ld;

        public allAdapter(LessonDlg ld) {
            this.ld = ld;
        }

        public void actionPerformed(ActionEvent e) {
            ld.selectAll();
        }

    }

    class noneAdapter implements ActionListener {

        protected LessonDlg ld;

        public noneAdapter(LessonDlg ld) {
            this.ld = ld;
        }

        public void actionPerformed(ActionEvent e) {
            ld.selectNone();
        }

    }

}
